import {
    graphql,
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLList,
    GraphQLNonNull,
    GraphQLBoolean,
    GraphQLInt

} from 'graphql';

import Sequelize from 'sequelize'

const Op = Sequelize.Op

import Device from '../../db/models/device'

import {
    deviceGroupByDepartment
} from '../../db/query'

export let deviceType = new GraphQLObjectType({
    name: 'device',
    fields: {
        id: {
            type: GraphQLInt
        },
        barcode: {
            type: GraphQLString
        },
        serial_number: {
            type: GraphQLString
        },
        kind_code: {
            type: GraphQLString
        },
        kind_name: {
            type: GraphQLString
        },
        company: {
            type: GraphQLString
        },
        department: {
            type: GraphQLString
        },
        brand: {
            type: GraphQLString
        },
        model: {
            type: GraphQLString
        },
        user_name: {
            type: GraphQLString
        },
        user_code: {
            type: GraphQLString
        },
        type: {
            type: GraphQLString
        },
        state: {
            type: GraphQLString
        },
        factory_time: {
            type: GraphQLString
        },
        scrap_time: {
            type: GraphQLString
        },
        remarks: {
            type: GraphQLString
        },
        isdel: {
            type: GraphQLInt
        },
        device_drive: {
            type: GraphQLString
        },
        device_peizhi: {
            type: GraphQLString
        },
        device_baoxiu: {
            type: GraphQLString
        }
    }
})

export const deviceList = {
    type: new GraphQLList(deviceType),
    args: {},
    resolve(root, params, options) {
        return Device.findAll()
    }
}

export const deviceListBy_User = {
    type: new GraphQLList(deviceType),
    args: {
        user_name: {
            type: GraphQLString
        },
        user_code: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {

        return Device.findAll({
            where: {
                user_name: params.user_name,
                user_code: params.user_code
            }
        })
    }
}
export const deviceListBy_Company_Department_ByPage = {
    type: new GraphQLList(deviceType),
    args: {
        company: {
            type: GraphQLString
        },
        department: {
            type: GraphQLString
        },
        offset: {
            type: GraphQLInt
        },
        limit: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        let where = {
            company: params.company,
            isdel: 0
        }
        if (params.department) {
            where.department = params.department
        }
        return Device.findAll({
            where: where,
            offset: params.offset,
            limit: params.limit

        })
    }
}

export const deviceListSearch = {
    type: new GraphQLList(deviceType),
    args: {
        key: {
            type: GraphQLString
        },
        company: {
            type: GraphQLString
        },
        department: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        let where = {
            isdel: 0,
            company: params.company,
            [Op.or]: [{
                    company: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    department: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    state: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    barcode: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    serial_number: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    kind_code: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    kind_name: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    brand: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    model: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    user_name: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    user_code: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    type: {
                        [Op.like]: '%' + params.key + '%'
                    }
                },
                {
                    remarks: {
                        [Op.like]: '%' + params.key + '%'
                    }
                }
            ]
        }
        if (params.department) {
            where.department = params.department
        }

        return Device.findAll({
            where: where
        })
    }
}

export const deviceListBy_Company_Department = {
    type: new GraphQLList(deviceType),
    args: {
        company: {
            type: GraphQLString
        },
        department: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        let where = {
            company: params.company,
            isdel: 0
        }
        if (params.department) {
            where.department = params.department
        }
        return Device.findAll({
            where: where
        })
    }
}

export const deviceList_Type_Count_By_Company_Department = {
    type: GraphQLInt,
    args: {
        type: {
            type: GraphQLString
        },

        company: {
            type: GraphQLString
        },
        department: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        let where = {
            company: params.company,
            type: params.type,
            isdel: 0
        }
        if (params.department) {
            where.department = params.department
        }
        return Device.count({
            where: where
        })
    }
}

export let deviceGroupType = new GraphQLObjectType({
    name: 'deviceGroup',
    fields: {
        num: {
            type: GraphQLInt
        },
        department: {
            type: GraphQLString
        }
    }
})

export const deviceGroupByDepartments = {
    type: new GraphQLList(deviceGroupType),
    args: {
        company: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return deviceGroupByDepartment(params)
    }
}


export const deviceOne = {
    type: deviceType,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Device.findOne({
            where: {
                id: params.id
            }
        })
    }
}

export const deviceUpdatePeizhi = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        },
        device_peizhi: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return Device.update({
            device_peizhi: params.device_peizhi
        }, {
            where: {
                id: params.id
            }
        })
    }
}

export const deviceUpdateBaoxiu = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        },
        device_baoxiu: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return Device.update({
            device_baoxiu: params.device_baoxiu
        }, {
            where: {
                id: params.id
            }
        })
    }
}

export const deviceUpdate = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        },
        user_name: {
            type: GraphQLString
        },
        user_code: {
            type: GraphQLString
        },
        department: {
            type: GraphQLString
        },
        company: {
            type: GraphQLString
        },
        type: {
            type: GraphQLString
        },
        state: {
            type: GraphQLString
        },
        remarks: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return Device.update({
            user_name: params.user_name,
            user_code: params.user_code.toUpperCase(),
            type: params.type,
            state: params.state,
            remarks: params.remarks,
            department: params.department,
            company: params.company,
        }, {
            where: {
                id: params.id
            }
        })
    }
}

export const deviceAdd = {
    type: deviceType,
    args: {
        barcode: {
            type: GraphQLString
        },
        serial_number: {
            type: GraphQLString
        },
        kind_code: {
            type: GraphQLString
        },
        kind_name: {
            type: GraphQLString
        },
        brand: {
            type: GraphQLString
        },
        model: {
            type: GraphQLString
        },
        user_name: {
            type: GraphQLString
        },
        user_code: {
            type: GraphQLString
        },
        department: {
            type: GraphQLString
        },
        company: {
            type: GraphQLString
        },
        type: {
            type: GraphQLString
        },
        state: {
            type: GraphQLString
        },
        factory_time: {
            type: GraphQLString
        },
        remarks: {
            type: GraphQLString
        }
    },
    resolve(root, params, options) {
        return Device.create({
            barcode: params.barcode,
            serial_number: params.serial_number,
            kind_code: params.kind_code,
            kind_name: params.kind_name,
            brand: params.brand,
            department: params.department,
            company: params.company,
            model: params.model,
            user_name: params.user_name,
            user_code: params.user_code.toUpperCase(),
            type: params.type,
            state: params.state,
            factory_time: params.factory_time,
            remarks: params.remarks,
            isdel: 0
        })
    }
}

export const deviceDel = {
    type: GraphQLInt,
    args: {
        id: {
            type: GraphQLInt
        }
    },
    resolve(root, params, options) {
        return Device.update({
            state: "报废",
            isdel: 1,
            scrap_time: new Date().toLocaleString()

        }, {
            where: {
                id: params.id
            }
        })
    }
}
